﻿using Dapper;
using Gx.Common.Core;
using Gx.Model.Entity;
using Gx.Model.ViewModel;
using System.Collections.Generic;
using System.Linq;
namespace Gx.Dal.Manage
{
    public class LKBillTemplateDal:BaseDal<LKBillTemplate>
    {
    
        /// <summary>
        /// 名称是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool AnyName(string name)
        {
            using (var conn = new DbContext())
            {
                var total = conn.Connection.QueryFirstOrDefault<int>("SELECT COUNT(1) FROM LK_BillTemplate WHERE IsDeleted=0 AND Name=@name", new { name });
                return total > 0;
            }
        }
        
        /// <summary>
        /// 获取最大编号
        /// </summary>
        /// <returns></returns>
        public string GetMaxLKBillTemplateCode()
        {
            using (var conn = new DbContext())
            {
                return conn.Connection.QueryFirstOrDefault<string>("SELECT MAX(LKBillTemplateCode) FROM LK_BillTemplate");
            }
        }

        /// <summary>
        /// 根据billtype获取模板
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public LKBillTemplate GetLKBillTemplateByBillType(int type) 
        {
            using (var conn = new DbContext())
            {
                return conn.Connection.QueryFirstOrDefault<LKBillTemplate>("SELECT * FROM LK_BillTemplate WHERE IsDeleted=0 AND BillType=@type", new { type });
            }
        }
    }
}

